package vns.client.presenter;

import java.util.ArrayList;
import java.util.List;

import vns.client.event.RuleSetEditEvent;
import vns.client.helper.RPCCall;
import vns.client.service.ObjectTypeServiceAsync;
import vns.client.service.RuleSetServiceAsync;
import vns.client.view.RuleSetUI;
import vns.shared.ObjectTypeDTO;
import vns.shared.RuleSetDTO;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class RuleSetPresenter implements Presenter {

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		doInitTable();
	}

	public interface Display {
		Button getUpdateButton();

		Button getEditButton();

		ArrayList<RuleSetDTO> getListRow();

		boolean setListRow(List<RuleSetDTO> listRow);

		List<ObjectTypeDTO> getListOType();

		void setListOType(List<ObjectTypeDTO> list);

		RuleSetDTO getCurrentRuleSetDTO();

		Widget asWidget();

		void render();
	}

	private final Display display;
	private final SimpleEventBus eventBus;
	private final RuleSetServiceAsync rpcService;
	private final ObjectTypeServiceAsync rpcOType;

	/*
	 * Public method
	 */

	public RuleSetPresenter(RuleSetUI view, SimpleEventBus eventBus,
			RuleSetServiceAsync model, ObjectTypeServiceAsync model2) {
		this.display = view;
		this.eventBus = eventBus;
		this.rpcService = model;
		this.rpcOType = model2;
		bind();
	}

	/*
	 * Private method
	 */

	private boolean bind() {
		display.getUpdateButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				doUpdate();
			}
		});
		display.getEditButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (display.getCurrentRuleSetDTO() != null)
					eventBus.fireEvent(new RuleSetEditEvent(display
							.getCurrentRuleSetDTO()));
			}
		});
		return false;
	}

	private void doInitTable() {
		new RPCCall<ArrayList<RuleSetDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Không tải được!" + caught.toString());
			}

			@Override
			public void onSuccess(ArrayList<RuleSetDTO> result) {
				display.setListRow(result);

				new RPCCall<ArrayList<ObjectTypeDTO>>() {

					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(ArrayList<ObjectTypeDTO> result) {
						display.setListOType(result);
						display.render();
					}

					@Override
					protected void callService(
							AsyncCallback<ArrayList<ObjectTypeDTO>> cb) {
						rpcOType.Loadall(cb);
					}
				}.retry(3);

			}

			@Override
			protected void callService(AsyncCallback<ArrayList<RuleSetDTO>> cb) {
				rpcService.Loadall(cb);
			}
		}.retry(3);
	}

	private void doUpdate() {

		new RPCCall<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Có lỗi khi đang cập nhật! \n "
						+ caught.toString());
			}

			@Override
			public void onSuccess(Void result) {
				Window.alert("Cập nhật thành công! \n ");
				doInitTable();
			}

			@Override
			protected void callService(AsyncCallback<Void> cb) {
				rpcService.Updateall(display.getListRow(), cb);
			}
		}.retry(3);
	}
}
